/******************************************/
/* DTD Inquisitor 2                       */
/* Copyright: University of Pennsylvania  */
/* Author: Byron Choi                     */
/* Date: 9/2001                           */
/* Contact: kkchoi@gradient.cis.upenn.edu */
/******************************************/
package automata;

import java.util.Hashtable;
import java.util.Enumeration;

public class Transitions extends Hashtable<State, Hashtable<Label, StateSet>> implements Cloneable {

	private static final long serialVersionUID = 1L;

	public Transitions() {
		super();
	}

	@SuppressWarnings("unchecked")
	public Object put(State s, Label l, StateSet ss) {
		Hashtable<Label, StateSet> h;
		if (containsKey(s)) {
			h = (Hashtable<Label, StateSet>) get(s);
		} else {
			h = new Hashtable<Label, StateSet>();
		}
		h.put(l, ss);
		return put(s, h);
	}

	public Object get(State s) {
		return super.get(s);
	}

	public Object get(State s, Label l) {
		return (((Hashtable<Label, StateSet>) (super.get(s))).get(l));
	}

	@SuppressWarnings("unchecked")
	public String toString() {
		String str = new String();
		Enumeration<State> e = keys();

		while (e.hasMoreElements()) {
			State s = (e.nextElement());
			str += toString_aux(s, (Hashtable<Label, StateSet>) (get(s)));
		}
		if(str.length()==0)//para imprimir transicao quando caminho do contexto = '/'
			str += "{1}";
		return str;
	}

	private String toString_aux(State s, Hashtable<Label, StateSet> h) {
		String str = new String();
		try {
			Enumeration<Label> e = h.keys();
			while (e.hasMoreElements()) {
				Label l = (e.nextElement());
				StateSet ds = (h.get(l));
				str += s.toString() + " -> " + l.toString() + " -> {"
						+ ds.toString() + "}\n";
			}
			return str;
		} catch (NullPointerException e) {
			return str;
		}
	}
	
	private String toString_aux_web(State s, Hashtable<Label, StateSet> h) {
		String str = new String();
		try {
			Enumeration<Label> e = h.keys();
			while (e.hasMoreElements()) {
				Label l = (e.nextElement());
				StateSet ds = (h.get(l));
				str += s.toString() + " -> " + l.toString() + " -> {"
						+ ds.toString() + "}<br>";
			}
			return str;
		} catch (NullPointerException e) {
			return str;
		}
	}

	@SuppressWarnings("unchecked")
	public Object clone() {
		Transitions t = new Transitions();
		Enumeration<State> e = this.keys();
		while (e.hasMoreElements()) {
			State k = e.nextElement();
			t.put(k, (Hashtable<Label, StateSet>) ((Hashtable<Label, StateSet>) get(k)).clone());
		}
		return t;
	}
}
